﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using PortalCurriculo.Domain.Utils;
using PortalCurriculo.WebUI.Helpers;

namespace PortalCurriculo.WebUI.UserControls.Basico
{
    public partial class DropDownList : UserControl
    {
        public string Text
        {
            get { return DropDown.Text; }
            set { DropDown.Text = value; }
        }

        public string SelectedValue
        {
            get { return DropDown.SelectedValue; }
            set { DropDown.SelectedValue = value; }
        }

        public string LabelText
        {
            get { return Label.Text; }
            set
            {
                Label.Visible = true;
                Label.Text = value;
            }
        }

        public bool Obrigatorio
        {
            get { return RequiredValidator.Visible; }
            set { RequiredValidator.Visible = value; }
        }

        public Unit Width { set { Panel.Width = value; } }

        public ServerValidateEventHandler ServerValidation
        {
            set { CustomValidator.ServerValidate += value; }
        }

        public void PreencherTodosAtivos<T>(bool opcaoSelecionar = false, string dataTextField = null) where T : GeneralAction, new()
        {
            var source = new GeneralAction().SelectAllActive<T>();
            Preencher(source, opcaoSelecionar, dataTextField);
        }

        /// <summary>
        /// Preenche DropDownList com todos os registros da tabela, mesmo se tiverem sido excluídos
        /// </summary>
        /// <typeparam name="T">Tabela </typeparam>
        /// <param name="opcaoSelecionar"></param>
        /// <param name="dataTextField"></param>
        public void PreencherTodos<T>(bool opcaoSelecionar = false, string dataTextField = null) where T : GeneralAction, new()
        {
            var source = new GeneralAction().SelectAll<T>();
            Preencher(source, opcaoSelecionar, dataTextField);
        }

        public void Preencher<T>(List<T> source, bool opcaoSelecionar, string dataTextField)
        {
            WebHelper.FillDropDownList(DropDown, source, opcaoSelecionar, dataTextField);

            if (!opcaoSelecionar) return;

            CustomValidator.ServerValidate += CustomValidator_ServerValidate;
            CustomValidator.Visible = true;
        }

        public string ClientValidationFunction
        {
            set
            {
                CustomValidator.Visible = true;
                CustomValidator.ClientValidationFunction += value;
            }
            get { return CustomValidator.ClientValidationFunction; }
        }

        public bool CustomValidateEmptyText
        {
            set
            {
                CustomValidator.ValidateEmptyText = value;
                CustomValidator.Visible = true;
            }
            get
            {
                return CustomValidator.ValidateEmptyText;
            }
        }

        public event ServerValidateEventHandler OnServerValidation
        {
            add
            {
                CustomValidator.ServerValidate += value;
                CustomValidator.Visible = true;
            }
            remove
            {
                value = null;
                CustomValidator.Visible = false;
            }
        }

        public string ValidationGroup
        {
            get { return DropDown.ValidationGroup; }
            set { DropDown.ValidationGroup = RequiredValidator.ValidationGroup = CustomValidator.ValidationGroup = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RequiredValidator.SetFocusOnError = true;
            CustomValidator.ServerValidate += Validar;
        }

        protected void Validar(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;
            CustomValidator.Visible = true;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            Label.AssociatedControlID = DropDown.ID;
            base.Render(writer);
        }

        protected void CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {

        }
    }
}
